<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/14
 * Time: 18:19
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Video extends Base
{
    /**
     * 赛事视频列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $mv_video_name = trim(input('mv_video_name'), ' ');
            $where['mv_committee_id'] = session('com_id');
            if(isset($mv_video_name) && $mv_video_name != ''){
                $where['mv_video_name'] = ['like', '%' . $mv_video_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_video')
                ->join('up_match', 'match_id = mv_match_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('match_video')
                ->join('up_match', 'match_id = mv_match_id', 'left')
                ->field('mv_id, mv_video_url, mv_video_name, mv_time, match_title')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('mv_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 上传赛事视频
     * @return mixed|\think\response\Json
     */
    public function videoAdd(){
        if(Request::instance()->isPost()){
            $mv_video_name = trim(input('mv_video_name'), ' ');
            $mv_video_url = input('video');
            $match = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->field('match_id, match_cate')
                ->find();
            if(!$match){
                return json(['code' => 100, 'msg' => '请先创建赛事']);
            }
            
            //转码的文件url
            $url = config('qiniu.domain') . $mv_video_url;//未转码之前的视频url，用于获取视频信息
            $video_info = file_get_contents($url . '?avinfo');
            $video_info = json_decode($video_info,true);
            $video_long_array = explode('.', $video_info['format']['duration']); // 时长
            $video_size = $video_info['format']['size']; // 大小
            // 转码
            $this->zhuanma($mv_video_url);
            $insert = [
                'mv_match_id' => $match['match_id'],
                'mv_video_cate' => $match['match_cate'],
                'mv_committee_id' => session('com_id'),
                'mv_video_url' => config('qiniu.domain') . 'video_' . $mv_video_url,
                'mv_video_name' => $mv_video_name,
                'mv_video_size' => $video_size,
                'mv_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_match_video')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '上传成功']);
            }else{
                return json(['code' => 100, 'msg' => '上传失败']);
            }
        }
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('video/videoadd');
        }
    }
    
    /**
     * 编辑赛事视频
     * @return mixed|\think\response\Json
     */
    public function videoEdit(){
        $mv_id = input('mv_id');
        if(Request::instance()->isPost()){
            $mv_video_name = trim(input('mv_video_name'), ' ');
            $mv_video_url = trim(input('video'), ' ');
            $update = [
                'mv_video_name' => $mv_video_name,
            ];
            if(!empty($mv_video_url)){
                //转码的文件url
                $url = config('qiniu.domain') . $mv_video_url;//未转码之前的视频url，用于获取视频信息
                $video_info = file_get_contents($url . '?avinfo');
                $video_info = json_decode($video_info,true);
                $video_long_array = explode('.', $video_info['format']['duration']); // 时长
                $video_size = $video_info['format']['size']; // 大小
                // 转码
                $this->zhuanma($mv_video_url);
                $update['mv_video_url'] = config('qiniu.domain') . 'video_' . $mv_video_url;
                $update['mv_video_size'] = $video_size;
//                $update['section_long_time'] = $video_long_array[0];
            }
            $res = Db::table('up_match_video')->where('mv_id', $mv_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $video = Db::table('up_match_video')
            ->where('mv_id', $mv_id)
            ->field('mv_match_id, mv_video_url, mv_video_name, mv_video_size')
            ->find();
        $this->assign([
            'matchVideo' => $video,
            'mv_id' => $mv_id
        ]);
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            return $this->fetch('video/videoedit');
        }
    }
    
    /**
     * 删除视频
     * @return \think\response\Json
     */
    public function videoDel(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $mv_id = input('id');
        $res = Db::table('up_match_video')->delete($mv_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}